//
//  CarTypePrice.m
//  AutoHome
//
//  Created by wqh on 15-5-14.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "CarTypePrice.h"

#import "CarTypePriceTopView.h"
#import "CarTypePriceTitleView.h"
#import "CarTypePriceGoShop.h"
#import "CarTypePriceCell.h"
#import "CarTypePriceModel.h"
#import "StoreAddress.h"
#import "AllStoreAddress.h"
#import "NetStroe.h"

#define kCarTypePriceCellId @"CarTypePriceCellId"

@interface CarTypePrice ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic) NSMutableArray *dataArr;

@property (nonatomic) CarTypePriceTopView *headerView;
@property (nonatomic) CarTypePriceTitleView *titleView;
@property (nonatomic) CarTypePriceGoShop *goShopView;

@property (nonatomic) UIButton *bottomBtn;
@property (nonatomic) NSMutableDictionary *carInfoDict;

@property (nonatomic) NSMutableArray *allAddressArr;
@property (nonatomic) NSMutableArray *minPriceArr;
@property (nonatomic) NSString *city;

@end

@implementation CarTypePrice

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.allAddressArr = [[NSMutableArray alloc] init];
    self.minPriceArr = [[NSMutableArray alloc] init];

    [self createHeaderView];
    [self createTitleView];
    [self createGoShopView];
    [self createTableView];
    [self createBottomBtn];
    [self initData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(displayMapButton:) name:@"displayButton" object:nil];
    
}

- (void)displayMapButton:(NSNotification *)isHidden{
    id object = [isHidden object];
    BOOL isH = [(NSNumber *)object  boolValue];
    self.bottomBtn.hidden = isH;
    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    if (self.bottomBtn) {
        [self.bottomBtn removeFromSuperview];
    }
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}



- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}   





- (void)createHeaderView {
    self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"CarTypePriceTopView" owner:self options:nil] lastObject];
    self.headerView.frame = CGRectMake(0, 0, kScreenWidth, 200);
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, self.view.bounds.size.height-64-44-49) style:UITableViewStylePlain];
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];

}


- (void)createTitleView {
    self.titleView = [[[NSBundle mainBundle] loadNibNamed:@"CarTypePriceTitleView" owner:self options:nil] lastObject];
    self.titleView.titleLabel.text = self.titleName;
}

- (void)createGoShopView {
    self.goShopView = [[[NSBundle mainBundle] loadNibNamed:@"CarTypePriceGoShop" owner:self options:nil] lastObject];
}

- (void)createBottomBtn {
    if (!self.bottomBtn) {
        self.bottomBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.bottomBtn setTitle:@"地图显示" forState:UIControlStateNormal];
        self.bottomBtn.frame = CGRectMake(0, kScreenHeight-49, kScreenWidth, 49);
        [[[UIApplication sharedApplication].delegate window] addSubview:self.bottomBtn];
        [self.bottomBtn setBackgroundImage:[UIImage imageNamed:@"tagBar_bg_click"] forState:UIControlStateNormal];
        [self.bottomBtn addTarget:self action:@selector(displayMapClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

- (void)displayMapClick:(UIButton *)button {
    button.hidden = YES;
//    [self removeFromParentViewController];
    AllStoreAddress *allAddressMap = [[AllStoreAddress alloc] init];
    allAddressMap.addressArr = self.allAddressArr;
    allAddressMap.minPriceArr = self.minPriceArr;
    
    allAddressMap.city = self.cityBtn.currentTitle;
   
    if (self.myPageJumpBlcok) {
        self.myPageJumpBlcok(allAddressMap);
    }
}

- (void)initData {
    
    [[DataEngine sharedInstance] requestCarTypeDetailInfoWithTypeId:self.typeId  cityId:self.cityId Success:^(id respondsData) {
        [self downloadDataSuccess:respondsData];
    } failed:^(NSError *error) {
        
    }];
    
    [[DataEngine sharedInstance] requestCarTypeDetailPriceWithSystemId:self.systemId typeId:self.typeId cityId:self.cityId Success:^(id respondsData) {
        [self downloadPriceDataSuccess:respondsData];
    } failed:^(NSError *error) {
        
    }];
}

- (void)downloadDataSuccess:(id)respondsData {
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:respondsData options:NSJSONReadingMutableContainers error:nil];
    
    self.carInfoDict = dict[@"result"];
    [self.headerView showDataWithDict:self.carInfoDict];
    if (self.titleView) {
        
        self.titleView.minPriceLabel.text = self.carInfoDict[@"minprice"];
    }
}

- (void)downloadPriceDataSuccess:(id)respondsData {
    if (self.dataArr) {
        [self.dataArr removeAllObjects];
    }else {
        self.dataArr = [NSMutableArray array];
    }
    NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:respondsData options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dictCarShop in dict[@"result"][@"dealerlist"]) {
        CarTypePriceModel *model = [CarTypePriceModel new];
        [model setValuesForKeysWithDictionary:dictCarShop];
        [self.dataArr addObject:model];
        [self.allAddressArr addObject:model.address];
        [self.minPriceArr addObject:model.price];
    }
    [self.tableView reloadData];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.dataArr.count+2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        UITableViewCell *cell    = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        cell.frame = CGRectMake(0, 0, kScreenWidth, 88);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell addSubview:self.titleView];
        return cell;
    }
    if (indexPath.row == 1) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [cell.contentView addSubview:self.goShopView];
        return cell;
    }else {
        
        CarTypePriceCell *cell =  [tableView dequeueReusableCellWithIdentifier:kCarTypePriceCellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CarTypePriceCell" owner:self options:nil] lastObject] ;
        }
        CarTypePriceModel *model = self.dataArr[indexPath.row-2];
        [cell showDataWithModle:model];
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.bottomBtn) {
        self.bottomBtn.hidden = YES;
    }
    if (indexPath.row == 1) {
        NetStroe *webView = [[NetStroe alloc] init];
        if (self.myPageJumpBlcok) {
            self.myPageJumpBlcok(webView);
        }
    }
    if (indexPath.row>1) {
        
        StoreAddress *mapView = [StoreAddress new];
        CarTypePriceModel *model = self.dataArr[indexPath.row-2];
        mapView.address = model.address;
        mapView.title = @"地图";
        mapView.titleName = model.shortname;
        mapView.minPrice = [NSString stringWithFormat:@"%@万",model.price];
        if (self.myPageJumpBlcok) {
            self.myPageJumpBlcok(mapView);
        }
    }
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 88;
    }
    if (indexPath.row == 1) {
        return 51;
    }
    return 156;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
